﻿using MES_SystemUI.Context;
using MES_SystemUI.Domain;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace MES_SystemUI.UI.DataDictUI.Stores
{
    public partial class StoresListForm : MES_SystemUI.UI.DataDictUI.DataBaseForm
    {
        public StoresListForm()
        {
            InitializeComponent();
            this.Load += StoresListForm_Load;
        }

        private void StoresListForm_Load(object sender, EventArgs e)
        {
            BindDataGridView();
        }
        public override void GetDataBind()
        {
            using (var ct = new Context.MES_SystemContext())
            {
                var que = from S in ct.stores.ToList()
                          join U1 in ct.users on S.Storekeeper equals U1.UserId
                          join Sc in ct.storeCategories on S.StoreCategoryId equals Sc.StoreCategorieId
                          join U2 in ct.users on S.CreateUserId equals U2.UserId
                          join U3 in ct.users on S.LastUpdateUserId equals U3.UserId into AllPc
                          from s in AllPc.DefaultIfEmpty()
                          select new
                          {
                              序号 = S.StoreId,
                              仓库编码 = S.StoreCode,
                              仓库名称 = S.StoreName,
                              仓库管理员 = U1.UserName,
                              仓库地址 = S.StoreAddress,
                              仓库类型 = Sc.StoreCategorieName,
                              状态 = S.Status == 0 ? "启用" : "禁用",
                              创建人 = U1.UserName,
                              创建时间 = S.CreateTime,
                              最后修改人 = s != null ? s.UserName : null,
                              最后修改时间 = S.LastUpdateTime,
                          };
                if (!string.IsNullOrEmpty(textBox1.Text.Trim()))
                {
                    que = que.Where(x => Regex.IsMatch(x.仓库名称, textBox1.Text));
                }
                if (!string.IsNullOrEmpty(dateTimePicker1.Text.Trim()))
                {
                    //DateTime dateTime1 = DateTime.Parse(dateTimePicker1.Value.ToString("yyyy-MM-dd") + " 00:00:00");
                    DateTime dateTime1 = DateTime.Parse(dateTimePicker1.Text + " 00:00:00");
                    que = que.Where(x => x.创建时间 >= dateTime1);
                }
                if (!string.IsNullOrEmpty(dateTimePicker2.Text.Trim()))
                {
                    //DateTime dateTime2 = DateTime.Parse(dateTimePicker2.Value.ToString("yyyy-MM-dd") + " 23:59:59");
                    DateTime dateTime2 = DateTime.Parse(dateTimePicker2.Text + " 23:59:59");
                    que = que.Where(x => x.创建时间 <= dateTime2);
                }
                if (comboBox1.Text != "全部")
                {
                    que = que.Where(x => x.状态 == comboBox1.Text);
                }
                Page = que.Count() % MainForm.SystemDataConfig.pageNumber == 0 ? que.Count() / MainForm.SystemDataConfig.pageNumber : que.Count() / MainForm.SystemDataConfig.pageNumber + 1;
                que = que.OrderByDescending(k => k.序号);
                que = que.Skip((NumberPage - 1) * MainForm.SystemDataConfig.pageNumber).Take(MainForm.SystemDataConfig.pageNumber);
                // 绑定数据
                dataGridView1.Columns.Clear();
                dataGridView1.DataSource = que.ToList();


            }
        }
        public override void OpenExcelCore(DataTable dt, MES_SystemContext c, int i)
        {
            Store mode = new Store()
            {
                StoreCode = dt.Rows[i][0].ToString(),
                StoreName = dt.Rows[i][1].ToString(),
                Storekeeper = int.Parse(dt.Rows[i][2].ToString()),
                StoreAddress = dt.Rows[i][3].ToString(),
                StoreCategoryId = int.Parse(dt.Rows[i][4].ToString()),
                Status = int.Parse(dt.Rows[i][5].ToString()),
                CreateUserId = int.Parse(dt.Rows[i][6].ToString()),
            };
            c.stores.Add(mode);
        }
        public override void StateCore(DataGridViewCellMouseEventArgs e, MES_SystemContext c, int state)
        {
            var Pc = c.stores.Find(int.Parse(dataGridView1.Rows[e.RowIndex].Cells["序号"].Value.ToString()));
            Pc.Status = state;
        }
    }
}
